package com.siberhus.tdfl;

import java.util.HashSet;
import java.util.Set;

import org.apache.commons.io.FilenameUtils;

public class DefaultDataFileHandler implements DataFileHandler {
	
	private DataFileReader reader;
	
	private ResourceCreator successResourceCreator = new DefaultResourceCreator("success");
	
	private DataFileWriter successWriter;
	
	private ResourceCreator errorResourceCreator = new DefaultResourceCreator("error");
	
	private DataFileWriter errorWriter;
	
	private boolean ignoreCase = true;
	
	private Set<String> extensions;
	
	public void setIgnoreCase(boolean ignoreCase){
		this.ignoreCase = ignoreCase;
	}
	
	@Override
	public boolean accept(String filename) {
		String extension = FilenameUtils.getExtension(filename);
		if(ignoreCase){
			return extensions.contains(extension.toUpperCase());
		}else{
			return extensions.contains(extension);
		}
	}
	
//	public void setExtensionsString(String extensions){
//		setExtensions(extensions.split(","));
//	}
	
	public void setExtensions(String[] extensions){
		this.extensions = new HashSet<String>();
		for(String ext : extensions){
			if(ignoreCase){
				this.extensions.add(ext.toUpperCase());
			}else{
				this.extensions.add(ext);
			}
		}
	}

	public DataFileReader getReader() {
		return reader;
	}

	public void setReader(DataFileReader reader) {
		this.reader = reader;
	}
	
	public ResourceCreator getSuccessResourceCreator() {
		return successResourceCreator;
	}

	public void setSuccessResourceCreator(ResourceCreator successResourceCreator) {
		this.successResourceCreator = successResourceCreator;
	}

	public DataFileWriter getSuccessWriter() {
		return successWriter;
	}
	
	public void setSuccessWriter(DataFileWriter successWriter) {
		this.successWriter = successWriter;
	}
	
	public ResourceCreator getErrorResourceCreator() {
		return errorResourceCreator;
	}

	public void setErrorResourceCreator(ResourceCreator errorResourceCreator) {
		this.errorResourceCreator = errorResourceCreator;
	}

	public DataFileWriter getErrorWriter() {
		return errorWriter;
	}

	public void setErrorWriter(DataFileWriter errorWriter) {
		this.errorWriter = errorWriter;
	}
	
}
